package com.xiaoyy.app.extend.util;

import cn.hutool.setting.dialect.Props;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.xiaoyy.core.common.vo.LoginVO;


public class AlipayUtil {
    public static LoginVO getAlipayInfo(LoginVO loginVO) {
        AlipayClient alipayClient=getAlipayClient();
        String authCode=loginVO.getLoginKey();
        try {
            AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
            request.setGrantType("authorization_code");
            request.setCode(authCode);
            AlipaySystemOauthTokenResponse response = alipayClient.execute(request);

            //调用成功，则处理业务逻辑
            if (response.isSuccess()) {
                System.out.println("调用成功");
                loginVO.setOpenId(response.getUserId());
                String accessToken=response.getAccessToken();

                AlipayUserInfoShareRequest shareRequest = new AlipayUserInfoShareRequest();
                AlipayUserInfoShareResponse shareResponse = alipayClient.execute(shareRequest,accessToken);
                if(shareResponse.isSuccess()){
                    System.out.println("支付宝身份信息调用成功");
                    loginVO.setUserName(shareResponse.getCertNo());//用户名 使用证件号
                    loginVO.setFullName(shareResponse.getUserName());//姓名
                    loginVO.setIdcard(shareResponse.getCertNo());//证件号码
                    loginVO.setCellphone(shareResponse.getMobile());//手机号

                    /*
                     * 0 身份证
                     * 1 护照
                     * 2 军官证
                     * 3 士兵证
                     * 4 回乡证
                     * 5 临时身份证
                     * 6 户口簿
                     * 7 警官证
                     * 8 台胞证
                     * 9 营业执照
                     * 10 其他证件
                     * 11 港澳居民来往内地通行证
                     * 12 台湾居民来往大陆通行证
                     * */
                    String CertType=shareResponse.getCertType();//支付宝证件类型
                    /*  系统
                    01	身份证      0、5
                    02	护照        1
                    03	台胞证      8
                    04	台湾居民来往大陆通行证  12
                    05	港澳居民往来内地通行证  11
                    06	外国人永久居留身份证
                    99  其他   2、3、4、6、7、9、10
                    */
                    String cardType="";//系统身份证
                    if(CertType.equals("0")||CertType.equals("5")){
                        cardType="01";
                    }else if(CertType.equals("1")){
                        cardType="02";
                    }else if(CertType.equals("8")){
                        cardType="03";
                    }else if(CertType.equals("12")){
                        cardType="04";
                    }else if(CertType.equals("11")){
                        cardType="05";
                    }else{
                        cardType="99";
                    }
                    loginVO.setCardType(cardType);//证件类型
                    loginVO.setUserType("20");//用户类型 个人
                } else {
                    System.out.println("支付宝身份信息调用失败");
                }
            } else {
                System.err.println("调用失败，原因：" + response.getMsg() + "，" + response.getSubMsg());
            }
        }catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }

        return loginVO;
    }


    //老版SDK
    public static AlipayClient getAlipayClient(){
        Props props = new Props("application.properties");
        String serverUrl = props.getProperty("system.config.alipay.url");
        String appId = props.getProperty("system.config.alipay.zhdy.appId");
        String merchantPrivateKey = props.getProperty("system.config.alipay.merchantPrivateKey");
        String alipayPublicKey = props.getProperty("system.config.alipay.alipayPublicKey");

        String format="json";
        String signType="RSA2";
        String CHARSET="GBK";
        String APP_ID=appId;
        String APP_PRIVATE_KEY=merchantPrivateKey;
        String ALIPAY_PUBLIC_KEY=alipayPublicKey;


        //实例化客户端
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, APP_ID, APP_PRIVATE_KEY, format, CHARSET, ALIPAY_PUBLIC_KEY, signType);
        return alipayClient;
    }

}
